<?php

namespace App\Http\Controllers;

use App\Models\Address;
use App\Models\Coupon;
use App\Models\CouponUser;
use App\Models\Goods;
use App\Models\Order;
use App\Models\User;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class OrderController extends Controller
{
    public function advance(Request $request)
    {
        $goods = Goods::where('id', $request->input('goods_id'))->first();
        $coupon_user = CouponUser::with(["coupon"])->where('user_id', $request->user()->id)->get();
        $address = Address::where('user_id', $request->user()->id)->where('status', 1)->first();
        return $this->response(compact('goods', 'coupon_user', 'address'));
    }

    public function index(Request $request)
    {
        $query = Order::with(['goods','seller'])->where('user_id', $request->user()->id)->orderBy('id', 'desc');
        if ($request->input('status') > -1) {
            $query = $query->where('status', $request->input('status'));
        }
        $data = $query->paginate();
        return $this->response($data);
    }

    public function view($id)
    {
        $obj = Order::with(['goods','seller'])->where('id',$id)->first();
        return $this->response($obj);
    }

    public function store(Request $request, Order $obj)
    {
        $goods = Goods::where('id', $request->input('goods_id'))->first();
        $address = Address::where('id', $request->input('address_id'))->first();
        $coupon_user_id = $request->input('coupon_user_id', 0);
        $num = 1;
        $amount_all = $goods->price * $num;
        $amount_les = 0;
        if ($coupon_user_id) {
            $amount_les = Coupon::getAmountLes($coupon_user_id, $amount_all);
        }
        $obj->fill([
            'order_no' => 'wx' . date('yymmddhhmmss'),
            'title' => $goods->title,
            'user_id' => $request->user()->id,
            'goods_id' => $goods->id,
            'coupon_user_id' => $coupon_user_id,
            'amount' => $amount_all - $amount_les,
            'amount_les' => $amount_les,
            'amount_all' => $amount_all,
            'price' => $goods->price,
            'num' => $num,
            'status' => 0,
            'address_name' => $address->name,
            'address_phone' => $address->phone,
            'address' => $address->address,
        ]);
        $obj->save();
        return $this->pay($obj->id);
    }

    public function update($id, Request $request)
    {
        $obj = Order::find($id);
        $data = $request->all();
        $data['status'] = 1;
        $obj->update($data);
        return $this->response($obj->id);
    }

    public function cancel($id, Request $request){
        $obj = Order::where('id', $id)->where('user_id', $request->user()->id)->first();
        $data['status'] = 9;
        $obj->update($data);
        return $this->response($obj->id);
    }

    public function destroy($id)
    {
        $obj = Order::find($id);
        $obj->delete();
        return $this->response();
    }

    public function pay($order_id)
    {
        $order = Order::where('id', $order_id)->first();
        if ($order->status !== 0) {
            abort(5074);
        }
        $user = User::where('id', $order->user_id)->first();
        $config = config('wechat');
        $app = Factory::payment($config);
        $result = $app->order->unify([
            'body' => $order->title,
            'out_trade_no' => $order->order_no,
            'total_fee' => $order->amount*100,
            'trade_type' => 'JSAPI',
            'openid' => $user->openid,
        ]);
        if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
            $jsSdkConfig = $app->jssdk->bridgeConfig($result['prepay_id']);
            $data = json_decode($jsSdkConfig, true);
            $data['order_id'] = $order_id;
            return $this->response($data);
        } else {
            Log::error('微信支付签名失败:' . var_export($result, 1));
            abort(5075);
        }
    }
}
